

library(tidyverse)
library(readr)
library(fuzzyjoin)
library(car)
library(stringr)
library(maps)
library(data.table)
library(cdlTools)
setwd(rprojroot::find_root(rprojroot::is_rstudio_project))

a <- fread("Intermediate Data/AdditionalFECReturns/1976-2022-house.csv")
a$district <- ifelse(a$district==0, 1, a$district)
#winners<-a %>% group_by(state, district, year) %>%
#  filter(candidatevotes==max(candidatevotes)) %>%
#  mutate(incumbent.voteshare = candidatevotes/totalvotes) %>% select(district, year, state_po, incumbent.voteshare)
#winners$mergeyear <- (winners$year-1)%/%2*2

demshare1<-a %>% group_by(state, district, year) %>%
  filter(party=="DEMOCRAT") %>% filter(candidatevotes==max(candidatevotes)) %>%
  mutate(dem.voteshare.lastup = candidatevotes/totalvotes) %>% select(district, year, state_po, dem.voteshare.lastup)
demshare1$mergeyear <- demshare1$year #(demshare1$year-1)%/%2*2

demshare2<-a %>% group_by(state, district, year) %>%
  filter(party=="DEMOCRAT") %>% filter(candidatevotes==max(candidatevotes)) %>%
  mutate(dem.voteshare.nextup = candidatevotes/totalvotes) %>% select(district, year, state_po, dem.voteshare.nextup)
demshare2$mergeyear <- demshare2$year-2


df<- fread("Data/house_vote_level_data_census.csv")
df$mergestate <- fips(df$state, "Abbreviation")
df$mergeyear <- (df$year-1)%/%2*2

policy<-left_join(df, demshare1, by = c("cd" = "district", "mergeyear" = "mergeyear", "mergestate" = "state_po"),
                  suffix = c("", ".y"))
policy<-left_join(policy, demshare2, by = c("cd" = "district", "mergeyear" = "mergeyear", "mergestate" = "state_po"),
                  suffix = c("", ".y"))
table(is.na(policy$dem.voteshare.lastup), policy$year)
table(is.na(policy$dem.voteshare.nextup), policy$year)

write_csv2(policy, "Data/house_vote_level_data_census.csv")



a <- fread("Intermediate Data/AdditionalFECReturns/1976-2020-president.csv")

#winners<-a %>% group_by(state, district, year) %>%
#  filter(candidatevotes==max(candidatevotes)) %>%
#  mutate(incumbent.voteshare = candidatevotes/totalvotes)
#winners$mergeyear <- (winners$year-1)%/%2*2

demshare1<-a %>% group_by(state, year) %>%
  filter(party_simplified=="DEMOCRAT") %>% filter(candidatevotes==max(candidatevotes)) %>%
  mutate(dem.presvote.lastup = candidatevotes/totalvotes) %>% select(year, state_po, dem.presvote.lastup)
demshare1$mergeyear <- demshare1$year

demshare2<-a %>% group_by(state, year) %>%
  filter(party_simplified=="DEMOCRAT") %>% filter(candidatevotes==max(candidatevotes)) %>%
  mutate(dem.presvote.nextup = candidatevotes/totalvotes) %>% select(year, state_po, dem.presvote.nextup)
demshare2$mergeyear <- demshare2$year

df<- fread("Data/senate_vote_level_data_census.csv")
df$mergestate <- fips(df$state, "Abbreviation")
df$mergeyear <- (df$year-1)%/%2*2

policy<-left_join(df, demshare1, join_by(mergestate==state_po, closest(mergeyear >= mergeyear)),
                  suffix = c("", ".y"))
policy<-left_join(policy, demshare2, join_by(mergestate==state_po, closest(mergeyear <= mergeyear)),
                  suffix = c("", ".y"))
table(is.na(policy$dem.presvote.lastup), policy$year)
table(is.na(policy$dem.presvote.nextup), policy$year)

  #select(-Date1.y)
write_csv2(policy, "Data/senate_vote_level_data_census.csv")
